<?php

    namespace App\Http\Controllers\V1_bak;

    use App\Http\Controllers\Controller;
    use App\Models\V1_bak\Order;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Cache;
    use Illuminate\Support\Facades\DB;

    class OrderController extends Controller
    {
        /**
         * 获取订单
         *
         * @param Request $request
         * author: walker
         * Date: 2019/10/24
         * Time: 16:05
         */
        public function getOrdersBySync(Request $request)
        {
            /**
             * 页码
             */
            $page      = $request->input('page') ?: 1;
            $pageCount = $request->input('page_count') ?: 1;
            /**
             * where条件
             */
            $orderId     = $request->input('order_id');
            $payStart    = $request->input('pay_start_time');
            $payEnd      = $request->input('pay_end_time');
            $updateStart = $request->input('update_start_time');
            $updateEnd   = $request->input('update_end_time');
            if ($pageCount > 1000 || $pageCount <= 0){
                $pageCount = 100;
            }
            $startPage = ($page - 1) * $pageCount;
            $where     = [];

            /**
             * 可选
             */
            if ( !empty($payStart)){
                $where[] = ['PayTime', '>', $payStart];
            }
            if ( !empty($payEnd)){
                $where[] = ['PayTime', '<', $payEnd];
            }
            if ( !empty($updateStart)){
                $where[] = ['UpdateTime', '>', $updateStart];
            }
            if ( !empty($updateEnd)){
                $where[] = ['UpdateTime', '<', $updateEnd];
            }
            $model       = new Order();
            $orderCount  = $model->countOrder($where);//订单统计
            $orderList   = $model->getOrderList($where, $startPage, $pageCount);//订单数据
            $orderList   = toArr($orderList);
            $countryList = Cache::get('countryList');
            if (empty($countryList)){
                $countryList = $model->getCountruList();
                Cache::add('countryList', $countryList);
            }

            //订单商品 订单日志
            foreach ($orderList as $key => $val) {
                $goodsList                               = toArr($model->getOrderGoods($val['OrderId']));
                $orderLog                                = toArr($model->getOrderLog($val['OrderId']));
                $orderList[$key]['orders_products_list'] = $goodsList;
                $orderList[$key]['order_log']            = $orderLog;
            }

            $outData = [
                'total_count' => $orderCount,
                'order'       => $orderList,
            ];
            //            $goods = $model->getOrderGoods()
            ajaxReturn(200, '获取成功', $outData);

        }

//        public function getOrderTotalPrice($orderData, $fee, $isMessage = 0)
//        {
//            //必要字段 UserDiscount用户折扣,ProductPrice订单原价,CouponDiscount优惠券折扣,
//            //ManageCurrency
//            //用户折扣
//            $userDiscount = ($orderData['UserDiscount'] > 0 && $orderData['UserDiscount'] < 100) ?: 100;
//            $prodPrice    = $orderData['ProductPrice'];//产品总价
//            $prodPrice    -= $orderData['ProductPrice'] * (1 - (100 - $orderData['Discount']) / 100);//商品折扣价
//            $prodPrice    -= $orderData['ProductPrice'] * (1 - $userDiscount / 100);//会员折扣
//            $prodPrice    -= $orderData['ProductPrice'] * $orderData['CouponDiscount'];//优惠券折扣
//
//            //汇率设置
//            $isRate = 0;
//            if ($orderData['Currency'] != $orderData['ManageCurrency'] && $orderData['Rate'] > 0){
//                $isRate = 1;
//            }
//
//            if($isMessage){
//                //后台固定默认货币显示
//                //总价 = 订单优惠后价格 + 运费价 + 运费险价格 -优惠券价格 -折扣价
//                $totalPrice =$prodPrice+$orderData['ShippingPrice']+$orderData['ShippingInsurancePrice']
//                    -$orderData['CouponPrice']
//                    -$orderData['DiscountPrice'];
//
//            }else{
//
//            }
//        }

    }
